package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponUserMapper;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.service.DtsCouponUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class DtsCouponUserServiceImpl implements DtsCouponUserService {
    @Autowired
    DtsCouponUserMapper couponUserMapper;
    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, Integer couponId, Integer userId, Integer status, String sort, String order) {
        IPage<DtsCouponUser> ipage = new Page<>(page, limit);
        QueryWrapper<DtsCouponUser> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(userId)) {
            wrapper.eq("user_id",userId);
        }
        if (!StringUtils.isEmpty(couponId)) {
            wrapper.eq("coupon_id",couponId);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status",status);
        }
        if ("desc".equals(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        couponUserMapper.selectPage(ipage,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("items", ipage.getRecords());
        data.put("total", ipage.getTotal());

        return data;
    }

    @Override
    public DtsCouponUser findById(Integer id) {
        DtsCouponUser dtsCouponUser = couponUserMapper.selectById(id);
        return dtsCouponUser;
    }
}